<?php
declare (strict_types=1);

namespace addons\V2\admin\model;

use app\common\model\Base;

class ProductSpecValue extends Base
{

    protected $name = 'addons_mall_product_spec_value';

    public function product()
    {
        return $this->hasOne(Product::class, 'id', 'pid')
            ->field('id,title,price,cate_1,cate_2,icon,stock,status,freight');
    }

    public static function getSettled($param, $user): array
    {
        try {
            $product = Product::where(['id' => $param['pid'], 'status' => 1])->find();
//            if ($product->cate_1 == 1) {
//                if ($user->is_active == 2) {
//                    return ['code' => -1, 'msg' => '只能购买一次'];
//                }
//            }elseif ($product->cate_1 == 2) {
//                if ($user->is_active == 1) {
//                    return ['code' => -1, 'msg' => '未激活不能购买'];
//                }
//            }
            if ($product->cate_1 == 2) {
                if ($user->level_id == 0) {
                    return ['code' => -1, 'msg' => '普通会员不能购买'];
                }
            }
            if (empty($product)) {
                return ['code' => -1, 'msg' => '商品已下架'];
            }
            if (!$param['num'] || $param['num'] < 1) {
                return ['code' => -1, 'msg' => '购买数量错误'];
            }
            if ($product->spec_open) {
                $specValue = parent::where('id', $param['spec_value_id'])->find();
                if (empty($specValue)) {
                    return ['code' => -1, 'msg' => '未查询到相关规格'];
                }
                if ($param['num'] > $specValue->stock) {
                    return ['code' => -1, 'msg' => '商品库存不足'];
                }

                if($product->cate_1==2)
                {
                    if($user->level_id)
                    {
                        $price = 'price_'.$user->level_id;
                        $vip_price = $specValue->$price;
                    }else{
                        $vip_price = $specValue->vip_price;
                    }
                }else{
                    $vip_price = $specValue->vip_price;
                }

                $data['product'] = [
                    [
                        'pid'           => $specValue->pid,
                        'title'         => $specValue->product->title,
                        'icon'          => $specValue->product->icon,
                        'spec_value_id' => $specValue->id,
                        'vip_price'     => $vip_price,
                        'sku_name'      => SpecItem::getSpecItemName($specValue->sku_id),
                        'num'           => $param['num'],
                        'cate_1'        => $specValue->product->cate_1
                    ]
                ];
                $data['total'] = $vip_price * $param['num'];
                $data['cate_1'] = [$specValue->product->cate_1];
            } else {
                if ($param['num'] > $product->stock) {
                    return ['code' => -1, 'msg' => '商品库存不足'];
                }
                $data['product'] = [
                    [
                        'pid'           => $product->id,
                        'title'         => $product->title,
                        'icon'          => $product->icon,
                        'vip_price'     => $product->vip_price,
                        'spec_value_id' => '',
                        'sku_name'      => '',
                        'num'           => $param['num'],
                        'cate_1'        => $product->cate_1
                    ]
                ];
                $data['cate_1'] = [$product->cate_1];
                $data['total'] = $product->vip_price * $param['num'];
            }
            $data['freightTotal'] = $product->freight * $param['num'];
            $data['realTotal'] = $data['total'] + $data['freightTotal'];
            return ['code' => 1, 'msg' => 'success', 'data' => $data];
        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => '服务器错误：' . $e->getMessage()];
        }
    }

    public static function setSpecValue($pid, $data)
    {
        try {
            parent::destroy(['pid' => $pid]);
            $all = [];
            $vip_price = [];
            $price = [];
            $stock = 0;
            foreach ($data as $k => $v) {
                $key['sku_id'] = $k;
                $key['pid'] = $pid;
                $key['stock'] = $v['stock'];
                $key['price'] = $v['price'];
                $key['vip_price'] = $v['vip_price'];
                $key['icon'] = $v['icon'];
                $all[] = $key;
                $stock += $v['stock'];
                $vip_price[] = $v['vip_price'];
                $price[] = $v['price'];
            }
            (new self())->insertAll($all);
            return [
                'stock'     => $stock,
                'vip_price' => min($vip_price),
                'price'     => min($price)
            ];
        } catch (\Exception $e) {
            dump($e->getMessage());die;
            return false;
        }
    }



    public static function setSpecValue2($pid, $data)
    {
        try {
            parent::destroy(['pid' => $pid]);
            $all = [];
            $price = [];
            $price_1 = [];
            $price_2 = [];
            $price_3 = [];
            $price_4 = [];
            $price_5 = [];
            $price_6 = [];
            $stock = 0;
            foreach ($data as $k => $v) {
                $key['sku_id'] = $k;
                $key['pid'] = $pid;
                $key['stock'] = $v['stock'];
                $key['price'] = $v['price'];
                $key['price_1'] = $v['price_1'];
                $key['price_2'] = $v['price_2'];
                $key['price_3'] = $v['price_3'];
                $key['price_4'] = $v['price_4'];
                $key['price_5'] = $v['price_5'];
                $key['price_6'] = $v['price_6'];
                $key['icon'] = $v['icon'];
                $all[] = $key;
                $stock += $v['stock'];
                $price[] = $v['price'];
                $price_1[] = $v['price_1'];
                $price_2[] = $v['price_2'];
                $price_3[] = $v['price_3'];
                $price_4[] = $v['price_4'];
                $price_5[] = $v['price_5'];
                $price_6[] = $v['price_6'];
            }
            (new self())->insertAll($all);
            return [
                'stock'   => $stock,
                'price'   => min($price),
                'price_1' => min($price_1),
                'price_2' => min($price_2),
                'price_3' => min($price_3),
                'price_4' => min($price_4),
                'price_5' => min($price_5),
                'price_6' => min($price_6),
            ];
        } catch (\Exception $e) {
            dump($e->getMessage());die;
            return false;
        }
    }

    public static function getSpecValue($pid)
    {
        try {

            $list = parent::where('pid', $pid)->select();
            $skuData = [];
            foreach ($list as $v) {
                $skuData['skus[' . $v['sku_id'] . '][stock]'] = $v['stock'];
                $skuData['skus[' . $v['sku_id'] . '][icon]'] = $v['icon'];
                $skuData['skus[' . $v['sku_id'] . '][price]'] = $v['price'];
                $skuData['skus[' . $v['sku_id'] . '][vip_price]'] = $v['vip_price'];
                $skuData['skus[' . $v['sku_id'] . '][price_1]'] = $v['price_1'];
                $skuData['skus[' . $v['sku_id'] . '][price_2]'] = $v['price_2'];
                $skuData['skus[' . $v['sku_id'] . '][price_3]'] = $v['price_3'];
                $skuData['skus[' . $v['sku_id'] . '][price_4]'] = $v['price_4'];
                $skuData['skus[' . $v['sku_id'] . '][price_5]'] = $v['price_5'];
                $skuData['skus[' . $v['sku_id'] . '][price_6]'] = $v['price_6'];
            }
            return $skuData;
        } catch (\Exception $e) {

            return [];
        }
    }
}
